import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/main_page/home/video/video_page.dart';
import 'package:flutter_douban/model/recipe_big_model.dart';
import 'package:flutter_douban/tool_views/cached_network_Image.dart';

import 'detail_header_author.dart';

class CookDetailHeaderView extends StatefulWidget {
  final RecipeBigModel model;
  const CookDetailHeaderView({Key? key, required this.model}) : super(key: key);

  @override
  _CookDetailHeaderViewState createState() => _CookDetailHeaderViewState();
}

class _CookDetailHeaderViewState extends State<CookDetailHeaderView> {
  @override
  Widget build(BuildContext context) {
    double height = 0;
    if (widget.model.ph != null &&
        widget.model.pw != null &&
        widget.model.ph! != '0' &&
        widget.model.pw! != '0') {
      height = (double.parse(widget.model.ph!) *
          MediaQuery.of(context).size.width /
          double.parse(widget.model.pw!));
    } else {
      height = MediaQuery.of(context).size.width / 3 * 2;
    }

    List<Widget> widgetList = [];
    if(widget.model.vfurl?.isNotEmpty == true) {
      widgetList.add(
          VideoPage(
            video_url: widget.model.vfurl!,
            video_cover_height: widget.model.ph!,
            video_cover_width: widget.model.pw!,
          )
      );
    }else{
      widgetList.add(
        CachLoadingImage(
            url: widget.model.original_photo_path!,
            width: MediaQuery.of(context).size.width,
            height: height),
      );
    }
    widgetList.add(_contentWidget());
    
    return Container(
      padding: EdgeInsets.only(top: 0, bottom: 20),
      child: Column(
        children: widgetList,
      ),
    );
  }

  Widget _contentWidget() {
    return Container(
      padding: EdgeInsets.only(left: 15, right: 15, top: 15, bottom: 0),
      alignment: Alignment.topLeft,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            widget.model.title!,
            style: TextStyle(
                fontSize: 20, color: Colors.black, fontWeight: FontWeight.w500),
          ),
          SizedBox(
            height: 6,
          ),
          Text(
            '精品  •  独家  •  浏览${widget.model.views_count_text}  •  收藏${widget.model.favo_counts_text}',
            style: TextStyle(
                fontSize: 12,
                color: Colors.black54,
                fontWeight: FontWeight.w400),
          ),
          SizedBox(
            height: 6,
          ),
          DetailHeaderAuthorView(
            model: widget.model.user,
          ),
          SizedBox(
            height: 6,
          ),
          Text(
            widget.model.cookstory?.length == 0
                ? '这个人很懒什么也没写'
                : widget.model.cookstory!,
            style: TextStyle(
                fontSize: 16,
                color: AppColors.black,
                fontWeight: FontWeight.w400),
            strutStyle:
                StrutStyle(forceStrutHeight: true, height: 1, leading: 1),
          ),
          SizedBox(
            height: 10,
          ),
          Container(
            padding: EdgeInsets.only(left: 36, right: 36),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                _rowWidget(
                    widget.model.cook_time!.length == 0
                        ? '暂无'
                        : widget.model.cook_time!,
                    'assets/images/common/recipe_time_native.png'),
                _rowWidget(widget.model.cook_difficulty!,
                    'assets/images/common/recipe_dif_native_easy.png')
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget _rowWidget(String nameStr, String imageStr) {
    List<Widget> widList = [];
    if (imageStr.contains('http') || imageStr.contains('https')) {
      widList.add(CachedNetworkImage(
        imageUrl: widget.model.cook_difficulty_image!,
        fit: BoxFit.cover,
        width: 24,
        height: 24,
      ));
    } else {
      widList.add(Image.asset(
        imageStr,
        width: 24,
        height: 24,
      ));
    }
    widList.add(SizedBox(
      width: 10,
    ));
    widList.add(Text(
      nameStr,
      style: TextStyle(
          fontSize: 16, color: Colors.black, fontWeight: FontWeight.bold),
    ));

    return Row(
      children: widList,
    );
  }
}
